package com.w_resume.todo;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.w_resume.entity.QueryRange;
import com.w_resume.entity.WEduExp;
import com.w_resume.entity.WJobExp;
import com.w_resume.entity.WProjExp;
import com.w_resume.entity.WResumeFinal;
import com.w_resume.mapper.WEduExpMapper;
import com.w_resume.mapper.WJobExpMapper;
import com.w_resume.mapper.WProjExpMapper;
import com.w_resume.mapper.WResumeFinalMapper;

public class writeToTxt {
	private static SqlSessionFactory ssf = null;

	static {
		String config = "SqlMapConfig.xml";
		InputStream in = null;
		try {
			in = Resources.getResourceAsStream(config);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ssf = new SqlSessionFactoryBuilder().build(in);

	}

	@Before
	public void set() throws IOException {
		String config = "SqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(config);
		ssf = new SqlSessionFactoryBuilder().build(in);

	}

	@Test
	public void write2Txt() {
		SqlSession ss = ssf.openSession();
		/* 查询w_resume_final表 */
		WResumeFinalMapper wrfm = ss.getMapper(WResumeFinalMapper.class);

		int low = wrfm.selectLowId();
		int high = wrfm.selectHighId();
		int num = 10000; /* 每次查询量 */
		int size = high - low + 1;
		int frequency = size / num; /* 查询次数 */
		//int mod = size % num;
		int begin = low, end;
		/* times */
		for (int i = 0; i < frequency; i++) {
			long t1 = System.currentTimeMillis();
			end = begin + num;
			dealwith(begin, end, wrfm,ss);
			begin = end + 1;
			long t2 = System.currentTimeMillis();
			System.out.println(t2-t1); 
			System.exit(0); 
		}

		dealwith(begin, high, wrfm,ss);

		 

	}
	
	private void dealwith(int begin, int end, WResumeFinalMapper wrfm, SqlSession ss) {

		QueryRange queryRange = new QueryRange();
		queryRange.setBegin(begin);
		queryRange.setEnd(end);
		List<WResumeFinal> wrfs = wrfm.selectResumeByRange(queryRange);
		int resumeId;
		for (WResumeFinal wrf : wrfs) {

			resumeId = wrf.getId();
			/* 根据resume_id查询w_edu_exp表 */
			WEduExpMapper weem = ss.getMapper(WEduExpMapper.class);
			List<WEduExp> wees = weem.selectByResumeId(resumeId);
			System.out.println(wees);

			/* 根据resume_id查询w_job_exp表 */
			WJobExpMapper wjem = ss.getMapper(WJobExpMapper.class);
			List<WJobExp> wjes = wjem.selectByResumeId(resumeId);
			System.out.println(wjes);

			/* 根据resume_id查询w_proj_exp表 */ 
			WProjExpMapper wpem = ss.getMapper(WProjExpMapper.class);
			List<WProjExp> wpes = wpem.selectByResumeId(resumeId);
			System.out.println(wpes);
			

		}

	}
	
	 
	public static void test(){
		SqlSession sqlSession = ssf.openSession();
		WResumeFinalMapper wrfm = sqlSession.getMapper(WResumeFinalMapper.class);
		WResumeFinal wrf = wrfm.selectByPrimaryKey(20);
		System.out.println(wrf);
		
	}

	public static void main(String[] args) {
		// writeToTxt w = new writeToTxt();
		// w.write2Txt();
		test();
	}

}
